package com.iw.core.security.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iw.core.orm.PageData;
import com.iw.core.security.entity.ResourcesGroup;
import com.iw.core.security.service.ResourcesGroupService;
import com.iw.core.utils.WebUtils;

@Controller
@RequestMapping("/security/resourcesGroup")
public class ResourcesGroupAction {
    @Resource
    private ResourcesGroupService resourcesGroupService;

    @RequestMapping("/list")
    public ModelMap list(HttpServletRequest request) {
        PageData<ResourcesGroup> pageData = new PageData<ResourcesGroup>();
        // 给pageData设置参数
        WebUtils.setPageDataParameter(request, pageData);
        pageData = resourcesGroupService.find(pageData);

        return new ModelMap(pageData);
    }

    @RequestMapping(value = "/new")
    public String addForm(Model model) {
        return "/security/resourcesGroup/input";
    }

    @RequestMapping(value = "/edit/{id}")
    public String editForm(@PathVariable("id") Integer id, Model model) {
        ResourcesGroup resourcesGroup = resourcesGroupService.find(id);
        model.addAttribute(resourcesGroup);
        return "/security/resourcesGroup/input";
    }

    @RequestMapping(value = "/all")
    @ResponseBody
    public List<ResourcesGroup> all(Model model) {
        List<ResourcesGroup> list = resourcesGroupService.findAll();
        return list;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(ResourcesGroup resourcesGroup) {
        if (resourcesGroup.isNew()) {
            resourcesGroupService.save(resourcesGroup);
        } else {
            resourcesGroupService.update(resourcesGroup);
        }

        return "redirect:/security/resourcesGroup/list";// 重定向
    }

    @RequestMapping(value = "/repeat")
    @ResponseBody
    public boolean repeat(String name, Integer id) {
        try {
            name = name.trim();
            List<ResourcesGroup> groups = resourcesGroupService.findList("name", name);
            if (groups != null && !groups.isEmpty()) {
                if (id != 0) {
                    ResourcesGroup r = resourcesGroupService.find(id);
                    if (!r.getName().equals(name)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        resourcesGroupService.delete(id);
        return "redirect:/security/resourcesGroup/list";// 重定向
    }

    @RequestMapping(value = "/visible/{id}")
    public String visible(@PathVariable("id") Integer id) {
        resourcesGroupService.visible(id);
        return "redirect:/security/resourcesGroup/list";// 重定向
    }

    @RequestMapping(value = "/unVisible/{id}")
    public String unVisible(@PathVariable("id") Integer id) {
        resourcesGroupService.unVisible(id);
        return "redirect:/security/resourcesGroup/list";// 重定向
    }

    @InitBinder
    public void InitBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }

}
